<?php

function rad($d) {
    return ($d * pi()) / 180.0;
}

function isArrayNotEmpty($array) {
    if ($array != false && count($array) > 0) {
        return true;
    }
    return false;
}

function convert2UTF8($content) {
    if (mb_check_encoding($content, "utf-8") != 1) {
        if (mb_check_encoding($content, "gbk") == 1) {
            $content = mb_convert_encoding($content, "utf-8", "gbk");
        } else {
            $content = mb_convert_encoding($content, "utf-8");
        }
    }
    return $content;
}

function isNull($str) {
    if ($str == null || $str == NULL || trim($str) == "null" || trim($str) == "NULL" || trim($str) == "") {
        return true;
    }
    return false;
}

function isNotNull($str) {
    if ($str == null || $str == NULL || trim($str) == "null" || trim($str) == "NULL" || trim($str) == "") {
        return false;
    }
    return true;
}

/**
 * 检查post或者get参数是否存在
 * @param type $array
 * @return boolean
 */
function checkParam(array $array) {
    foreach ($array as $value) {
        if (!isset($_POST[$value]) && !isset($_GET[$value])) {
            return false;
        }
    }
    return true;
}

function _curl_post($url, $vars) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "var=" . $vars);
    $data = curl_exec($ch);
    curl_close($ch);
    if ($data) {
        return $data;
    } else {
        return false;
    }
}

function _curl_get($url) {
    // create curl resource
    $ch = curl_init();
    // set url
    curl_setopt($ch, CURLOPT_URL, $url);
    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // $output contains the output string
    $output = curl_exec($ch);
    // close curl resource to free up system resources
    curl_close($ch);
    if ($output) {
        return $output;
    } else {
        return false;
    }
}

function array_sort($array, $column) {
    if (!is_array($array)) {
        return false;
    }
    $count = count($array);
    if ($count == 0) {
        return array();
    } elseif ($count == 1) {
        return $array;
    } elseif ($count > 1) {
        return bubbleSort($array, $column, $count);
    }
}

function bubbleSort($array, $column, $count) {
    for ($i = 0; $i < $count; $i++) {
        for ($j = $count - 1; $j > $i; $j--) {
            if ($array[$j][$column] < $array[$j - 1][$column]) {
                $temp = $array[$j];
                $array[$j] = $array[$j - 1];
                $array[$j - 1] = $temp;
            }
        }
    }
    return $array;
}

//返回两点距离，单位米
function getDistance($lat1, $lng1, $lat2, $lng2) {
    $radLat1 = rad(doubleval($lat1));
    $radLat2 = rad(doubleval($lat2));
    $a = $radLat1 - $radLat2;
    $b = rad(doubleval($lng1)) - rad(doubleval($lng2));

    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
    $s = $s * EARTH_RADIUS;
    $s = round($s * 10000) / 10000;
    return $s;
}

/**
 * 远程获取数据，POST模式
 * 注意：
 * 1.使用Crul需要修改服务器中php.ini文件的设置，找到php_curl.dll去掉前面的";"就行了
 * 2.文件夹中cacert.pem是SSL证书请保证其路径有效，目前默认路径是：getcwd().'\\cacert.pem'
 * @param $url 指定URL完整路径地址
 * @param $cacert_url 指定当前工作目录绝对路径
 * @param $para 请求的数据
 * @param $input_charset 编码格式。默认值：空值
 * return 远程输出的数据
 */
function getHttpResponsePOST($url, $cacert_url, $para, $input_charset = '') {

    if (trim($input_charset) != '') {
        $url = $url . "_input_charset=" . $input_charset;
    }
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); //SSL证书认证
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); //严格认证
    curl_setopt($curl, CURLOPT_CAINFO, $cacert_url); //证书地址
    curl_setopt($curl, CURLOPT_HEADER, 0); // 过滤HTTP头
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 显示输出结果
    curl_setopt($curl, CURLOPT_POST, true); // post传输数据
    curl_setopt($curl, CURLOPT_POSTFIELDS, $para); // post传输数据
    $responseText = curl_exec($curl);
    //var_dump( curl_error($curl) );//如果执行curl过程中出现异常，可打开此开关，以便查看异常内容
    curl_close($curl);

    return $responseText;
}

function getParameter($para) {
    if (isset($_SESSION[$para])) {
        $value = trim($_SESSION[$para]);
    } else if (isset($_POST [$para])) {
        $value = trim($_POST [$para]);
    } else if (isset($_GET[$para])) {
        $value = trim($_GET [$para]);
    } else {
        $value = "";
    }
    return $value;
}


function parsePara($array) {
    $keys = array_keys($array);
    $para = "";
    for ($i = 0; $i < count($keys); $i++) {
        $para .= $keys[$i] . "=" . $array[$keys[$i]] . "&";
    }
    return $para;
}

/**
 * 除去数组中的空值和签名参数
 * @param $para 签名参数组
 * return 去掉空值与签名参数后的新签名参数组
 */
function paraFilter($para) {
    $para_filter = array();
    while (list ($key, $val) = each($para)) {
        if ($key == "sign" || $key == "sign_type" || $val == "")
            continue;
        else
            $para_filter[$key] = $para[$key];
    }
    return $para_filter;
}

/**
 * 对数组排序
 * @param $para 排序前的数组
 * return 排序后的数组
 */
function argSort($para) {
    ksort($para);
    reset($para);
    return $para;
}

/* *
 * 支付宝接口公用函数
 * 详细：该类是请求、通知返回两个文件所调用的公用函数核心处理文件
 * 版本：3.3
 * 日期：2012-07-19
 * 说明：
 * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
 * 该代码仅供学习和研究支付宝接口使用，只是提供一个参考。
 */

/**
 * 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
 * @param $para 需要拼接的数组
 * return 拼接完成以后的字符串
 */
function createLinkstring($para) {
    $arg = "";
    while (list ($key, $val) = each($para)) {
        $arg.=$key . "=" . $val . "&";
    }
    //去掉最后一个&字符
    $arg = substr($arg, 0, count($arg) - 2);

    //如果存在转义字符，那么去掉转义
    if (get_magic_quotes_gpc()) {
        $arg = stripslashes($arg);
    }

    return $arg;
}

/**
 * 写日志，方便测试（看网站需求，也可以改成把记录存入数据库）
 * 注意：服务器需要开通fopen配置
 * @param $word 要写入日志里的文本内容 默认值：空值
 */
function logResult($word = '') {
    $fp = fopen("log.txt", "a");
    flock($fp, LOCK_EX);
    fwrite($fp, "执行日期：" . strftime("%Y%m%d%H%M%S", time()) . "\n" . $word . "\n");
    flock($fp, LOCK_UN);
    fclose($fp);
}

/**
 * 实现多种字符编码方式
 * @param $input 需要编码的字符串
 * @param $_output_charset 输出的编码格式
 * @param $_input_charset 输入的编码格式
 * return 编码后的字符串
 */
function charsetEncode($input, $_output_charset, $_input_charset) {
    $output = "";
    if (!isset($_output_charset))
        $_output_charset = $_input_charset;
    if ($_input_charset == $_output_charset || $input == null) {
        $output = $input;
    } elseif (function_exists("mb_convert_encoding")) {
        $output = mb_convert_encoding($input, $_output_charset, $_input_charset);
    } elseif (function_exists("iconv")) {
        $output = iconv($_input_charset, $_output_charset, $input);
    } else
        die("sorry, you have no libs support for charset change.");
    return $output;
}

/**
 * 实现多种字符解码方式
 * @param $input 需要解码的字符串
 * @param $_output_charset 输出的解码格式
 * @param $_input_charset 输入的解码格式
 * return 解码后的字符串
 */
function charsetDecode($input, $_input_charset, $_output_charset) {
    $output = "";
    if (!isset($_input_charset))
        $_input_charset = $_input_charset;
    if ($_input_charset == $_output_charset || $input == null) {
        $output = $input;
    } elseif (function_exists("mb_convert_encoding")) {
        $output = mb_convert_encoding($input, $_output_charset, $_input_charset);
    } elseif (function_exists("iconv")) {
        $output = iconv($_input_charset, $_output_charset, $input);
    } else
        die("sorry, you have no libs support for charset changes.");
    return $output;
}

/* *
 * 支付宝接口RSA函数
 * 详细：RSA签名、验签、解密
 * 版本：3.3
 * 日期：2012-07-23
 * 说明：
 * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
 * 该代码仅供学习和研究支付宝接口使用，只是提供一个参考。
 */

/**
 * RSA签名
 * @param $data 待签名数据
 * @param $private_key_path 商户私钥文件路径
 * return 签名结果
 */
function rsaSign($data, $private_key_path) {
    $priKey = file_get_contents($private_key_path);
    $res = openssl_get_privatekey($priKey);
    openssl_sign($data, $sign, $res);
    openssl_free_key($res);
    //base64编码
    $sign = base64_encode($sign);
    return $sign;
}

/**
 * RSA验签
 * @param $data 待签名数据
 * @param $ali_public_key_path 支付宝的公钥文件路径
 * @param $sign 要校对的的签名结果
 * return 验证结果
 */
function rsaVerify($data, $ali_public_key_path, $sign) {
    $pubKey = file_get_contents($ali_public_key_path);
    $res = openssl_get_publickey($pubKey);
    $result = (bool) openssl_verify($data, base64_decode($sign), $res);
    openssl_free_key($res);
    return $result;
}

/**
 * RSA解密
 * @param $content 需要解密的内容，密文
 * @param $private_key_path 商户私钥文件路径
 * return 解密后内容，明文
 */
function rsaDecrypt($content, $private_key_path) {
    $priKey = file_get_contents($private_key_path);
    $res = openssl_get_privatekey($priKey);
    //用base64将内容还原成二进制
    $content = base64_decode($content);
    //把需要解密的内容，按128位拆开解密
    $result = '';
    for ($i = 0; $i < strlen($content) / 128; $i++) {
        $data = substr($content, $i * 128, 128);
        openssl_private_decrypt($data, $decrypt, $res);
        $result .= $decrypt;
    }
    openssl_free_key($res);
    return $result;
}

/* *
 * MD5
 * 详细：MD5加密
 * 版本：3.3
 * 日期：2012-07-19
 * 说明：
 * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
 * 该代码仅供学习和研究支付宝接口使用，只是提供一个参考。
 */

/**
 * 签名字符串
 * @param $prestr 需要签名的字符串
 * @param $key 私钥
 * return 签名结果
 */
function md5Sign($prestr, $key) {
    $prestr = $prestr . $key;
    return md5($prestr);
}

/**
 * 验证签名
 * @param $prestr 需要签名的字符串
 * @param $sign 签名结果
 * @param $key 私钥
 * return 签名结果
 */
function md5Verify($prestr, $sign, $key) {
    $prestr = $prestr . $key;
    $mysgin = md5($prestr);

    if ($mysgin == $sign) {
        return true;
    } else {
        return false;
    }
}

/**
 * 获取返回时的签名验证结果
 * @param $para_temp 通知返回来的参数数组
 * @param $sign 返回的签名结果
 * @param $isSort 是否对待签名数组排序
 * @return 签名验证结果
 */
function RSASignVeryfy($para_temp, $sign, $isSort, $publicKey) {
    //除去待签名参数数组中的空值和签名参数
    $para = paraFilter($para_temp);

    //对待签名参数数组排序
    if ($isSort) {
        $para = argSort($para);
    } else {
        $para = $this->sortNotifyPara($para);
    }

    //把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
    $prestr = createLinkstring($para);
    $isSgin = false;
    switch ("RSA") {
        case "MD5" :
            $isSgin = md5Verify($prestr, $sign, $this->alipay_config['key']);
            break;
        case "RSA" :
            $isSgin = rsaVerify($prestr, $publicKey, $sign);
            break;
        case "0001" :
            $isSgin = rsaVerify($prestr, $publicKey, $sign);
            break;
        default :
            $isSgin = false;
    }

    return $isSgin;
}

/**
 * 发送微信消息
 * @param type $openId
 * @param type $content
 */
function sendSimpleWeixinMsg($openId, $content) {
    $msg = "openId=" . $openId . "&appId=" . AppId . "&secretKey=" . AppSecret . "&content=" . $content;
    $sign = urlencode(rsaSign($msg, PUSH_PRIVATE_KEY));
    $cacert_url = "";
    $para = $msg . "&sign=" . $sign;
    return getHttpResponsePOST(PUSH_URL, $cacert_url, $para);
}

function sendTemplateWeixinMsg($openId, $content, $type) {
    $msg = "openId=" . $openId . "&appId=" . AppId . "&secretKey=" . AppSecret . "&content=" . $content . "&type=" . $type;
    $sign = urlencode(rsaSign($msg, PUSH_PRIVATE_KEY));
    $cacert_url = "";
    $para = $msg . "&sign=" . $sign;
    return getHttpResponsePOST(PUSH_URL, $cacert_url, $para);
}

function isBadAccount($accunique){
	$array = array(
        16415
        ,15308
        ,15294
        ,16421
        ,8709
        ,16387
        ,16450
        ,10320
        ,15293
        ,15292
        ,16448
        ,16420
        ,5962
        ,16449
        ,12084
        ,16419
        ,15304
        ,15295
        ,16451
        ,8300
        ,15296
        ,15303
        ,16417
        ,15302
        ,13401
        ,15297
        ,15300
        ,9634
        ,15298
        ,15299
        ,15305
        ,16416
        ,8351
        ,9638
        ,8352
        ,12083
        ,9632
        ,9646
        ,8350
        ,12481
        ,13518
        ,9639
        ,9648
        ,11798
        ,11692
        ,12942
        ,13517
        ,13516
        ,10007
        ,16425
        ,16426
        ,16422
        ,16424
        ,11693
        ,15306
    );
    for($i = 0; $i < count($array); $i++){
        if($accunique == $array[$i]){
            return true;
        }
    }
    return false;
}

/**
 * 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串，并对字符串做urlencode编码
 * @param $para 需要拼接的数组
 * return 拼接完成以后的字符串
 */
function createLinkstringUrlencode($para) {
    $arg = "";
    while (list ($key, $val) = each($para)) {
        $arg.=$key . "=" . urlencode($val) . "&";
    }
    //去掉最后一个&字符
    $arg = substr($arg, 0, count($arg) - 2);

    //如果存在转义字符，那么去掉转义
    if (get_magic_quotes_gpc()) {
        $arg = stripslashes($arg);
    }

    return $arg;
}

/**
 * 远程获取数据，GET模式
 * 注意：
 * 1.使用Crul需要修改服务器中php.ini文件的设置，找到php_curl.dll去掉前面的";"就行了
 * 2.文件夹中cacert.pem是SSL证书请保证其路径有效，目前默认路径是：getcwd().'\\cacert.pem'
 * @param $url 指定URL完整路径地址
 * @param $cacert_url 指定当前工作目录绝对路径
 * return 远程输出的数据
 */
function getHttpResponseGET($url, $cacert_url) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, 0); // 过滤HTTP头
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 显示输出结果
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); //SSL证书认证
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); //严格认证
    curl_setopt($curl, CURLOPT_CAINFO, $cacert_url); //证书地址
    $responseText = curl_exec($curl);
    //var_dump( curl_error($curl) );//如果执行curl过程中出现异常，可打开此开关，以便查看异常内容
    curl_close($curl);

    return $responseText;
}

/**
 * 获得微信JS API签名
 * @param DBC $db
 * @param type $noceStr
 * @param type $timestamp
 * @param type $url
 * @return type
 */
function getWeixinJsApiSignature(DBC $db, $noceStr, $timestamp, $url) {
    $ticketArray = getWeixinCommonValueValidByKey($db, "jsapi_ticket"); //获得保存有效的js api ticket
    $tickeOK = true;
    if (isArrayNotEmpty($ticketArray)) {//找到有效的ticket
        $jsapi_ticket = $ticketArray[0]['WEIXIN_VALUE'];
    } else {//没找到有效的ticket
        $tickeOK = false;
    }

    if (!$tickeOK) {//没找到有效ticket
        $tokenArray = getWeixinCommonValueValidByKey($db, "access_token"); //获得有效的access token
        $tokenOK = true;
        if (isArrayNotEmpty($tokenArray)) {//找到有效的access token
            $access_token = $tokenArray[0]['WEIXIN_VALUE'];
        } else {//找不到有效的access token
            $tokenOK = false;
        }
        if ($tokenOK) {
            $jsapi_ticket_json = getWeixinJSApiTicket($access_token); //根据accesstoken获得ticket
        } else {
            $access_token_json = getWeixinAccessToken(AppId, AppSecret); //先获得accesstoken
            updateWeixinCommonValue($db, "access_token", $access_token_json['access_token'], $access_token_json['expires_in']);
            $access_token = $access_token_json['access_token'];
            $jsapi_ticket_json = getWeixinJSApiTicket($access_token); //再获得ticket
        }
        $jsapi_ticket = $jsapi_ticket_json['ticket'];
        updateWeixinCommonValue($db, "jsapi_ticket", $jsapi_ticket_json['ticket'], $jsapi_ticket_json['expires_in']);
    }

    $array = array();
    $array['noncestr'] = $noceStr;
    $array['jsapi_ticket'] = $jsapi_ticket;
    $array['timestamp'] = $timestamp;
    $array['url'] = $url;

    $para = argSort($array);
    $prestr = createLinkstring($para);
    $signature = sha1($prestr);
    return $signature;
}

//获得微信ACCESS TOKEN，如果过期就更新
function getWeixinAccessTokenOrUpdateAccessToken(DBC $db) {
    $tokenArray = getWeixinCommonValueValidByKey($db, "access_token"); //获得有效的access token
    $tokenOK = true;
    if (isArrayNotEmpty($tokenArray)) {//找到有效的access token
        $access_token = $tokenArray[0]['WEIXIN_VALUE'];
        if (trim($access_token) == "") {
            $tokenOK = false;
        }
    } else {//找不到有效的access token
        $tokenOK = false;
    }
    if ($tokenOK) {
        
    } else {
        $access_token_json = getWeixinAccessToken(AppId, AppSecret); //先获得accesstoken
        updateWeixinCommonValue($db, "access_token", $access_token_json['access_token'], $access_token_json['expires_in']);
        $access_token = $access_token_json['access_token'];
    }

    return $access_token;
}

//更新ACCESS TOKEN，并返回
function updateAccessToken(DBC $db) {
    $access_token_json = getWeixinAccessToken(AppId, AppSecret); //先获得accesstoken
    updateWeixinCommonValue($db, "access_token", $access_token_json['access_token'], $access_token_json['expires_in'], true);
    return $access_token_json['access_token'];
}

/**
 * 获得微信ACCESS TOKEN等夏新
 * @param type $appkey
 * @param type $appsecret
 * @return type
 */
function getWeixinAccessToken($appkey, $appsecret) {
    $accessurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appkey . "&secret=" . $appsecret;
    $vars = "";
    $accesstokens = weixin_curl_post($accessurl, $vars);
    return json_decode($accesstokens, true);
}

/**
 * 获得微信js接口信息
 * @param type $accessToken
 * @return type
 */
function getWeixinJSApiTicket($accessToken) {
    $js_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" . $accessToken . "&type=jsapi";
    $vars = "";
    $jstokens = weixin_curl_post($js_url, $vars);
    return json_decode($jstokens, true);
}

/**
 * post访问url
 * @param type $url
 * @param type $vars
 * @return type
 */
function weixin_curl_post($url, $vars) {
    $ch = curl_init(); //初始化curl
    curl_setopt($ch, CURLOPT_URL, $url); //设置链接
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设置是否返回信息
    curl_setopt($ch, CURLOPT_HEADER, 0); //设置HTTP头
    curl_setopt($ch, CURLOPT_POST, 1); //设置为POST方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $vars); //POST数据
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    $response = curl_exec($ch); //接收返回信息
    if (curl_errno($ch)) {//出错则显示错误信息
        print curl_error($ch);
    }
    curl_close($ch); //关闭curl链接
    return $response;
}

/**
 * 检查设备时间是否过期
 * @param type $serviceEndDate
 * @return boolean
 */
function checkDeviceExpire($serviceEndDate) {
    $ok = true;
    if ((strtotime($serviceEndDate) + 86400) < time()) { // 过期  86400为一天的秒数
        $ok = false;
    }
    return $ok;
}

/**
 * 计算年龄
 * @param type $mydate
 * @return type
 */
function getAgeByBirthday($mydate) {
    $birth = $mydate;
    list($by, $bm, $bd) = explode('-', $birth);
    $cm = date('n');
    $cd = date('j');
    $age = date('Y') - $by - 1;
    if ($cm > $bm || $cm == $bm && $cd > $bd)
        $age++;
    return $age;
}

function pagination($url, $currentPage, $totalPages) {
    $paginationHtml = "";
    if ($currentPage == "1") {//第一页
    } else if ($currentPage == $totalPages) {//最后一页
    }
}

function paginate_three($reload, $page, $tpages, $adjacents) {

    $prevlabel = "&lsaquo; 上一页";
    $nextlabel = "下一页 &rsaquo;";

    $out = "<div class=\"pagin\">\n";

    // previous
    if ($page == 1) {
        $out.= "<span>" . $prevlabel . "</span>\n";
    } elseif ($page == 2) {
        $out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
    } else {
        $out.= "<a href=\"" . $reload . "&amp;page=" . ($page - 1) . "\">" . $prevlabel . "</a>\n";
    }

    // first
    if ($page > ($adjacents + 1)) {
        $out.= "<a href=\"" . $reload . "\">1</a>\n";
    }

    // interval
    if ($page > ($adjacents + 2)) {
        $out.= "...\n";
    }

    // pages
    $pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
    $pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
    for ($i = $pmin; $i <= $pmax; $i++) {
        if ($i == $page) {
            $out.= "<span class=\"current\">" . $i . "</span>\n";
        } elseif ($i == 1) {
            $out.= "<a href=\"" . $reload . "\">" . $i . "</a>\n";
        } else {
            $out.= "<a href=\"" . $reload . "&amp;page=" . $i . "\">" . $i . "</a>\n";
        }
    }

    // interval
    if ($page < ($tpages - $adjacents - 1)) {
        $out.= "...\n";
    }

    // last
    if ($page < ($tpages - $adjacents)) {
        $out.= "<a href=\"" . $reload . "&amp;page=" . $tpages . "\">" . $tpages . "</a>\n";
    }

    // next
    if ($page < $tpages) {
        $out.= "<a href=\"" . $reload . "&amp;page=" . ($page + 1) . "\">" . $nextlabel . "</a>\n";
    } else {
        $out.= "<span>" . $nextlabel . "</span>\n";
    }

    $out.= "</div>";

    return $out;
}

/**
 * 特殊字符转码
 * @param type $text
 * @return type
 */
function convertSpeicalCharacter($text) {
    $text1 = str_replace("&", "/amp/", $text);
    $text2 = str_replace("=", "/equal/", $text1);
    return $text2;
}

//增加返回角色昵称方法
function getJobName($job)
{
	if($job == 0)
	{
		$jobname = '新手';
	}elseif($job == 1)
	{
		$jobname = '骑士';
	}elseif($job == 2)
	{
		$jobname = '弓箭手';
	}elseif($job == 3)
	{
		$jobname = '术士';
	}elseif($job == 4)
	{
		$jobname = '精灵';
	}elseif($job == 5)
	{
		$jobname = '法师';
	}elseif($job == 6)
	{
		$jobname = '牧师';
	}elseif($job == 7)
	{
		$jobname = '圣骑士';
	}elseif($job == 8)
	{
		$jobname = '圣武士';
	}elseif($job == 9)
	{
		$jobname = '火影猎人';
	}elseif($job == 10)
	{
		$jobname = '幻影弓手';
	}elseif($job == 11)
	{
		$jobname = '幽灵法师';
	}elseif($job == 12)
	{
		$jobname = '圣洁牧师';
	}elseif($job == 13)
	{
		$jobname = '全部';
	}elseif($job == 30)
	{
		$jobname = '杜特';
	}elseif($job == 31)
	{
		$jobname = '斗士';
	}elseif($job == 32)
	{
		$jobname = '暗骑士';
	}elseif($job == 33)
	{
		$jobname = '暗刺客';
	}elseif($job == 34)
	{
		$jobname = '灵魂猎人';
	}elseif($job == 35)
	{
		$jobname = '召唤师';
	}elseif($job == 36)
	{
		$jobname = '祭师';
	}elseif($job == 40)
	{
		$jobname = '皇家骑士';
	}elseif($job == 41)
	{
		$jobname = '刀锋剑士';
	}elseif($job == 42)
	{
		$jobname = '赏金猎人';
	}elseif($job == 43)
	{
		$jobname = '魅影猎者';
	}elseif($job == 44)
	{
		$jobname = '元素法神';
	}elseif($job == 45)
	{
		$jobname = '神圣牧师';
	}elseif($job == 46)
	{
		$jobname = '炼狱游侠';
	}elseif($job == 47)
	{
		$jobname = '泰坦狂刀';
	}elseif($job == 48)
	{
		$jobname = '潜伏者';
	}elseif($job == 49)
	{
		$jobname = '战斗巡狩者';
	}elseif($job == 50)
	{
		$jobname = '次元使者';
	}elseif($job == 51)
	{
		$jobname = '黑暗祭师';
	}else{
		$jobname = '获取中...';
	}
	$data = array(
	    0 => "新手",
        1 => "骑士",
        2 => "弓箭手",
        3 => "术士",
        4 => "",

    );
	return $jobname;
}

//返回需要查询的数据库字段
function getAlljob($job)
{
	$jobarray = array();
	if($job == 40)
	{
		$job1 = 0;
		$job2 = 1;
		$job3 = 7;
		$job4 = 40;
	}elseif($job==41)
	{
		$job1 = 0;
		$job2 = 1;
		$job3 = 8;
		$job4 = 41;
	}elseif($job==42)
	{
		$job1 = 0;
		$job2 = 2;
		$job3 = 9;
		$job4 = 42;
	}elseif($job==43)
	{
		$job1 = 0;
		$job2 = 2;
		$job3 = 10;
		$job4 = 43;
	}elseif($job==44)
	{
		$job1 = 4;
		$job2 = 5;
		$job3 = 11;
		$job4 = 44;
	}elseif($job==45)
	{
		$job1 = 4;
		$job2 = 6;
		$job3 = 12;
		$job4 = 45;
	}elseif($job==46)
	{
		$job1 = 30;
		$job2 = 31;
		$job3 = 46;
		$job4 = -2;
	}elseif($job==47)
	{
		$job1 = 30;
		$job2 = 32;
		$job3 = 47;
		$job4 = -2;
	}elseif($job==48)
	{
		$job1 = 30;
		$job2 = 33;
		$job3 = 48;
		$job4 = 0;
	}elseif($job==49)
	{
		$job1 = 30;
		$job2 = 34;
		$job3 = 49;
		$job4 = -2;
	}elseif($job==50)
	{
		$job1 = 30;
		$job2 = 35;
		$job3 = 50;
		$job4 = -2;
	}elseif($job==51)
	{
		$job1 = 30;
		$job2 = 36;
		$job3 = 51;
		$job4 = -2;
	}elseif($job==7)
	{
		$job1 = 0;
		$job2 = 1;
		$job3 = 7;
		$job4 = -2;
	}elseif($job==8)
	{
		$job1 = 0;
		$job2 = 1;
		$job3 = 8;
		$job4 = -2;
	}elseif($job==9)
	{
		$job1 = 0;
		$job2 = 2;
		$job3 = 9;
		$job4 = -2;
	}elseif($job==10)
	{
		$job1 = 0;
		$job2 = 2;
		$job3 = 10;
		$job4 = -2;
	}elseif($job==11)
	{
		$job1 = 4;
		$job2 = 5;
		$job3 = 11;
		$job4 = -2;
	}elseif($job==12)
	{
		$job1 = 4;
		$job2 = 6;
		$job3 = 12;
		$job4 = -2;
	}elseif($job==31)
	{
		$job1 = 30;
		$job2 = 31;
		$job3 = 0;
		$job4 = 0;
	}elseif($job==32)
	{
		$job1 = 30;
		$job2 = 32;
		$job3 = -2;
		$job4 = -2;
	}elseif($job==33)
	{
		$job1 = 30;
		$job2 = 33;
		$job3 = -2;
		$job4 = -2;
	}elseif($job==34)
	{
		$job1 = 30;
		$job2 = 34;
		$job3 = -2;
		$job4 = -2;
	}elseif($job==35)
	{
		$job1 = 30;
		$job2 = 35;
		$job3 = -2;
		$job4 = -2;
	}elseif($job==36)
	{
		$job1 = 30;
		$job2 = 36;
		$job3 = -2;
		$job4 = -2;
	}elseif($job==5)
	{
		$job1 = 4;
		$job2 = 5;
		$job3 = -2;
		$job4 = -2;
	}elseif($job==6)
	{
		$job1 = 4;
		$job2 = 6;
		$job3 = -2;
		$job4 = -2;
	}elseif($job==1)
	{
		$job1 = 0;
		$job2 = 1;
		$job3 = -2;
		$job4 = -2;
	}elseif($job==2)
	{
		$job1 = 0;
		$job2 = 2;
		$job3 = -2;
		$job4 = -2;
	}else
	{
		$job1 = -1;
		$job2 = -1;
		$job3 = -1;
		$job4 = -1;
	}
	$jobarray[0] = $job1;
	$jobarray[1] = $job2;
	$jobarray[2] = $job3;
	$jobarray[3] = $job4;
	return $jobarray;
}

function getJavaServerQrCodeUrl($payAccount, $payGame, $payGameVer, $payGameOpt, $payAmount){
    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1:1888/api/caem/recharge/generateQrUrl');
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 1);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    //设置post数据
    $post_data = array(
        "payAccount" => $payAccount,
        "payGame" => $payGame,
        "payGameVer" => $payGameVer,
        "payGameOpt" => $payGameOpt,
        "payAmount" => $payAmount
        );
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    //显示获得的数据
    print_r($data);
    return $data;
}

function objtoarr($obj)
{
    $ret = array();
    foreach ($obj as $key => $value) {
        if (gettype($value) == 'array' || gettype($value) == 'object') {
            $ret[$key] = objtoarr($value);
        } else {
            $ret[$key] = $value;
        }
    }
    return $ret;
}

function ch_json_encode($data)
{
    function ch_urlencode($data)
    {
        if (is_array($data) || is_object($data)) {
            foreach ($data as $k => $v) {
                if (is_scalar($v)) {
                    if (is_array($data)) {
                        $data[$k] = urlencode($v);
                    } else if (is_object($data)) {
                        $data->$k = urlencode($v);
                    }
                } else if (is_array($data)) {
                    $data[$k] = ch_urlencode($v); //递归调用该函数
                } else if (is_object($data)) {
                    $data->$k = ch_urlencode($v);
                }
            }
        }
        return $data;
    }

    $ret = ch_urlencode($data);
    $ret = json_encode($ret);
    return urldecode($ret);
}
function doCurlPostRequest($url = '', Array $data = array())
{
    // $data_string = json_encode($data,JSON_UNESCAPED_UNICODE);
    $data_string = ch_json_encode($data);
    // $data_string = $data;
    $curl_con = curl_init();
    curl_setopt($curl_con, CURLOPT_URL,$url);
    curl_setopt($curl_con, CURLOPT_HEADER, false);
    curl_setopt($curl_con, CURLOPT_POST, true);
    curl_setopt($curl_con, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl_con, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($curl_con, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
    );
    curl_setopt($curl_con, CURLOPT_POSTFIELDS, $data_string);
    $res = curl_exec($curl_con);
    $status = curl_getinfo($curl_con);
    curl_close($curl_con);
    // print_r($res);
    if (isset($status['http_code']) && $status['http_code'] == 200) {
        $content = json_decode($res);
        $content_arr = objtoarr($content);
        return $content_arr;
    } else {
        return FALSE;
    }
}